package com.baidu.fastXML;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * json转对象数组
 */

public class TestJsonList {
    public static void main(String[] args) throws Exception {
        String jsonStr = "[\n" +
                "{\n" +
                "    \"eventVersion\":\"63\",\n" +
                "    \"eventId\":\"trail-event-iamusercreate-8e4b1d22131646ba92736c6a1dfc54a7-1652859532000\",\n" +
                "    \"eventType\":\"普通事件\",\n" +
                "    \"eventSource\":\"iam\",\n" +
                "    \"eventName\":\"CreateUser\",\n" +
                "    \"eventTimeInMilliseconds\":1652859532000,\n" +
                "    \"eventTime\":\"2022-05-18T07:38:52Z\",\n" +
                "\n" +
                "     \"userIdentity\":{\n" +
                "        \"iamDomainId\":\"33f35c23a0c848b5b9afb3ad35903f08\",\n" +
                "        \"loginUserId\":\"\",\n" +
                "        \"userDisplayName\":\"sunyumei_2\"\n" +
                "    },\n" +
                "    \"userIpAddress\":\"116.237.219.206\",\n" +
                "    \"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3_1) AppleWebKit/537.3\",\n" +
                "\n" +
                "    \"regionId\":\"bj\",\n" +
                "    \"requestId\":\"8e4b1d22131646ba92736c6a1dfc54a7\",\n" +
                "\n" +
                "    \"apiVersion\":\"1.0.0\",\n" +
                "    \"description\":\"\",\n" +
                "    \"errorCode\":\"200\",\n" +
                "    \"errorMessage\":\"\",\n" +
                "    \"eventDetail\":{\n" +
                "        \"additionalEventData\":{\n" +
                "            \"api\":\"POST /api/iam/user/create\",\n" +
                "            \"host\":\"bjhw-bce-online-product-console0.bjhw.baidu.com\",\n" +
                "            \"method\":\"POST\",\n" +
                "            \"cost\":\"120\"\n" +
                "        }\n" +
                "    },\n" +
                "\n" +
                "    \"resources\":[\n" +
                "        {\n" +
                "            \"resourceType\":\"resource-User\",\n" +
                "            \"resourceName\":\"fa8e87604637446c81df7e03ec053093\"\n" +
                "        }\n" +
                "    ],\n" +
                "\n" +
                "    \"success\":true\n" +
                "},\n" +
                "{\n" +
                "    \"eventVersion\":\"63\",\n" +
                "    \"eventId\":\"trail-event-iamusercreate-8e4b1d22131646ba92736c6a1dfc54a7-1652859532000\",\n" +
                "    \"eventType\":\"普通事件\",\n" +
                "    \"eventSource\":\"iam\",\n" +
                "    \"eventName\":\"CreateUser\",\n" +
                "    \"eventTimeInMilliseconds\":1652859532000,\n" +
                "    \"eventTime\":\"2022-05-18T07:38:52Z\",\n" +
                "\n" +
                "     \"userIdentity\":{\n" +
                "        \"iamDomainId\":\"33f35c23a0c848b5b9afb3ad35903f08\",\n" +
                "        \"loginUserId\":\"\",\n" +
                "        \"userDisplayName\":\"sunyumei_2\"\n" +
                "    },\n" +
                "    \"userIpAddress\":\"116.237.219.206\",\n" +
                "    \"userAgent\":\"Mozilla/5.0 (Macintosh; Intel Mac OS X 12_3_1) AppleWebKit/537.3\",\n" +
                "\n" +
                "    \"regionId\":\"bj\",\n" +
                "    \"requestId\":\"8e4b1d22131646ba92736c6a1dfc54a7\",\n" +
                "\n" +
                "    \"apiVersion\":\"1.0.0\",\n" +
                "    \"description\":\"\",\n" +
                "    \"errorCode\":\"200\",\n" +
                "    \"errorMessage\":\"\",\n" +
                "    \"eventDetail\":{\n" +
                "        \"additionalEventData\":{\n" +
                "            \"api\":\"POST /api/iam/user/create\",\n" +
                "            \"host\":\"bjhw-bce-online-product-console0.bjhw.baidu.com\",\n" +
                "            \"method\":\"POST\",\n" +
                "            \"cost\":\"120\"\n" +
                "        }\n" +
                "    },\n" +
                "\n" +
                "    \"resources\":[\n" +
                "        {\n" +
                "            \"resourceType\":\"resource-User\",\n" +
                "            \"resourceName\":\"fa8e87604637446c81df7e03ec053093\"\n" +
                "        }\n" +
                "    ],\n" +
                "\n" +
                "    \"success\":true\n" +
                "}\n" +
                "]";

        ObjectMapper objectMapper = new ObjectMapper();
        //1.json转对象
        List<Event> events = (List<Event>) objectMapper.readValue(jsonStr, objectMapper.getTypeFactory().constructParametricType(ArrayList.class, Event.class));

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        events.stream().forEach(event -> {
            System.out.println(simpleDateFormat.format(event.getEventTime().getTime()) + " 时间戳转换的: " + simpleDateFormat.format(new Date(event.getEventTimeInMilliseconds())));
        });

    }
}
